package com.formula.propertyshop.view.phone;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.formula.propertyshop.R;
import com.formula.propertyshop.model.Course;
import com.formula.propertyshop.service.IWireFrameService;
import com.formula.propertyshop.service.WireFrameServiceFactory;
import com.formula.propertyshop.utils.Const;
import com.formula.propertyshop.utils.WFErrorCode;
import com.formula.propertyshop.utils.WFUtils;
import com.formula.propertyshop.view.common.BaseActivity;

public class CourseDetailActivity extends BaseActivity{
	private Button btnRegister;
	private TextView txtName;
	private TextView txtDate;
	private TextView txtPrice;
	private TextView txtLeft;
	private Course course;
	private int id=0;
	public void onCreate(Bundle saveInstance){
		super.onCreate(saveInstance);
		if(!WFUtils.checkInternetConnection(this)){
			showNetworkErrorDlg();
			return;
		}
		TextView title = (TextView) findViewById(R.id.txtTitle);
		title.setText(getString(R.string.cource));
		btnRegister = (Button) findViewById(R.id.btnRegister);
		txtName = (TextView) findViewById(R.id.txtName);
		txtDate = (TextView) findViewById(R.id.txtDate);
		txtPrice = (TextView) findViewById(R.id.txtPrice);
		txtLeft = (TextView) findViewById(R.id.txtLeft);
		
		setControls();	
		loadData();
	}
	@Override
	protected void initUI() {
		setContentView(R.layout.course_detail);
		
	}
	private void loadData(){
		showProgressDlg();
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				id = getIntent().getIntExtra(Const.PROPERTYID,0);
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();
				Message msg = handler.obtainMessage();
				try {
					course = service.getCourse(id);		
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					msg.arg1 = WFErrorCode.NETWORK_ERROR.getValue();
				}
				msg.obj = course;
				handler.sendMessage(msg);
			}
		});
		thread.start();
	}
	private Handler handler = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();
			if(id==0||course==null){
				return;
			}
			try {
				txtName.setText(course.getName());
				txtDate.setText(getString(R.string.start_Date)+" : "+ course.getDate());
				txtPrice.setText(getString(R.string.price)+" : "+ course.getStringPrice());
				txtLeft.setText(getString(R.string.left)+" : "+ course.getLeft());
			} catch(Exception e){}
		}
	};
	
	private void setControls(){
		btnRegister.setOnClickListener(new View.OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CourseDetailActivity.this,ContactActivity.class);
				intent.putExtra(Const.COURSEID, course.getId());
				intent.putExtra(Const.ACTIVITY_ACTION, Const.ACTION_REGISTER_COURSE);
				startActivity(intent);
			}
			
		});
	}
}
